#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
# -*- coding:utf-8 -*-
import os
import sys
import unittest
from pathlib import Path
import coverage
from coverage import CoverageException

suite = unittest.TestSuite()
BASE_PATH = Path(__file__).parents[1]
sys.path.insert(0, str(BASE_PATH))
sys.path.insert(1, str(BASE_PATH.joinpath("check_in")))
TEST_CASE_PATH = BASE_PATH.joinpath("test")
os.environ["CI-SETTINGS"] = str(TEST_CASE_PATH.joinpath("mock_data", "config.yaml"))
cov = coverage.coverage(
    data_suffix="init",
    include=[str(BASE_PATH.joinpath("check_in", "cli", "*"))],
    data_file=str(BASE_PATH.parents[0].joinpath(".coverage")),
)


def specify_case(file_path):
    """
    Test specify test cases
    Args:
        file_path: test cases file path

    Returns: discover result
    """
    discover = unittest.defaultTestLoader.discover(
        file_path, pattern="test*.py", top_level_dir=file_path
    )
    return discover


if __name__ == "__main__":
    runner = unittest.TextTestRunner()
    args = sys.argv
    cov.start()
    test_case_files = [
        str(TEST_CASE_PATH.joinpath("analysis")),
        str(TEST_CASE_PATH.joinpath("build")),
        str(TEST_CASE_PATH.joinpath("check")),
        str(TEST_CASE_PATH.joinpath("comment")),
        str(TEST_CASE_PATH.joinpath("install")),
        str(TEST_CASE_PATH.joinpath("pull_link")),
    ]
    errors = []
    failures = []
    for file in test_case_files:
        runner_result = runner.run(specify_case(file))
        errors.extend(runner_result.errors)
        failures.extend(runner_result.failures)

    if any([errors, failures]):
        sys.exit(1)

    cov.stop()
    try:
        cov.report(show_missing=True)
        # cov.html_report()
    except CoverageException:
        print("No data to report")
        sys.exit(1)
